package com.sun.dingDing.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;

public class DingDingUtils {
    private static OkHttpClient client = new OkHttpClient();
    public static boolean sendToDingDing(String jsonString,String webHooK){
        try {
            String type = "application/json;charset=utf-8";
            RequestBody body = RequestBody.create(jsonString,MediaType.parse(type));
            Request.Builder builder = new Request.Builder()
                    .url(webHooK)
                    .post(body)
                    .addHeader("Content-Type",type);
            Request request = builder.build();
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            System.out.printf("send ding message:%s\n",string);
            JSONObject res = JSONObject.parseObject(string);
            return  res.getIntValue("errcode") == 0;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
